import Vue from 'vue'
import router from './router'
import store from './store'
//拦截器设置全局请求token
Vue.http.interceptors.push((request, next) => {
		if(localStorage.getItem('userInfo')!="null"&&localStorage.getItem('userInfo')!=""&&localStorage.getItem('userInfo')!=null){
			request.headers.set('Authorization',JSON.parse(localStorage.getItem('userInfo')).token); //设置请求头
			request.headers.set('Content-Type','application/json;charset=UTF-8'); //设置请求头
		}
		// request.headers.set('us','web'); //设置web后台标识
		//设置请求时间
		let timeout;
		// 如果某个请求设置了_timeout,那么超过该时间，会终端该（abort）请求,并执行请求设置的钩子函数onTimeout方法，不会执行then方法。
		if (request._timeout) {
			timeout = setTimeout(() => {
				if(request.onTimeout) {
					request.onTimeout(request);
					request.abort()
				}  
			}, request._timeout);
		}
		next((response) => {
			clearTimeout(timeout);
			return response
		})
})